package com.example.carbon.controller;

import com.example.carbon.Dto.energy.Search;
import com.example.carbon.pojo.energy.EnergyEfficiency;
import com.example.carbon.pojo.RestBean;
import com.example.carbon.server.info.EnergyEfficiencyInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/energy")
@Slf4j
public class EnergyEfficiencyController {
    @Resource
    EnergyEfficiencyInfo energyEfficiencyInfo;

    public int i=0;
    @GetMapping("getall")
    public RestBean getAllInfo(){
        log.info(""+i);
        i++;
        return RestBean.success(energyEfficiencyInfo.getAllEnergyEfficiencyInfo());
    }

    @PostMapping("/set")
    public RestBean setInfo(@RequestBody EnergyEfficiency energyEfficiency){
        if (energyEfficiencyInfo.updateEnergyInfo(energyEfficiency) ){
            return RestBean.success("更改成功");
        }
        return RestBean.failure(500,"更改失败");
    }

    @PostMapping("/insert")
    public RestBean insertInfo(@RequestBody EnergyEfficiency energyEfficiency){
        if (energyEfficiencyInfo.insertNewEnergyEfficiency(energyEfficiency)){
            return RestBean.success("添加成功");
        }
        return RestBean.failure(500,"添加失败");
    }

    @RequestMapping("/del")
    public RestBean delInfo(int id){
        if (energyEfficiencyInfo.deleteEnergyEfficiency(id)){
            return RestBean.success("删除成功");
        }
        return RestBean.failure(500,"删除失败");
    }

    @RequestMapping("/search")
    public RestBean search(@RequestBody Search search){
        try {
            if (search == null){
                return RestBean.failure(400,"搜索内容为空");
            }
            return RestBean.success(energyEfficiencyInfo.searchKey(search));
        }catch (Exception e){
            return RestBean.failure(500,"搜索失败");
        }

    }
}
